/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* JPie.java
*
* Created on November 12, 2007, 1:25 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.swing.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;

/**
 *
 * @author stewari1
 */


//requires classfile: PieData
public class JPie extends JComponent {

    /** Field description */
    private final int[] values;

    /** Field description */
    private final Color[] colors;

    /** Field description */
    private final int sliceNumber;

    /** Field description */
    private final PieValue[] slices;

    /** Field description */
    private Dimension area;

    JPie(int[] values, Color[] colors) {

        if (values.length != colors.length) {

            System.out.println("values and colors must match in length.. exiting");
            System.exit(0);
        }

        this.values      = values;
        this.colors      = colors;
        this.sliceNumber = values.length;
        slices           = new PieValue[sliceNumber];

        for (int i = 0; i < sliceNumber; i++) {

            slices[i] = new PieValue(values[i], colors[i]);
        }
    }

    public void setPreferredSize(Dimension area) {
        this.area = area;
    }

    public Dimension getPreferredSize() {
        return area;
    }

    public void paint(Graphics g) {


        // Draw the pie
        // drawPie((Graphics2D)g, new Rectangle(area),slices);
        drawPie((Graphics2D) g, getBounds(), slices);
    }

    void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {


        // slices is an array of values that represent the size of each slice.
        // Get total value of all slices
        double total = 0.0D;

        for (int i = 0; i < slices.length; i++) {

            total += slices[i].value;
        }


        // Draw each pie slice
        double curValue = 0.0D;
        int startAngle  = 0;

        for (int i = 0; i < slices.length; i++) {


            // Compute the start and stop angles
            startAngle = (int) (curValue * 360 / total);

            int arcAngle = (int) (slices[i].value * 360 / total);


            // Ensure that rounding errors do not leave a gap between the first and last slice
            if (i == slices.length - 1) {
                arcAngle = 360 - startAngle;
            }


            // Set the color and draw a filled arc
            g.setColor(slices[i].color);
            g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

            curValue += slices[i].value;
        }
    }

    /**
     * Class description
     *
     * @version        Enter version here..., 12/11/23
     * @author         Susanta Tewari
     */
    private class PieValue {


        // Class to hold a value for a slice
        final double value;
        final Color color;

        /**
         * Constructs ...
         *
         * @param value description
         * @param color description
         */
        public PieValue(double value, Color color) {

            this.value = value;
            this.color = color;
        }
    }
}
